/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.planeteVelo.bean;

import com.planeteVelo.backedBean.Famille;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author guig
 */
@Named
@RequestScoped
public class FamilleBean {

    private Famille famille = new Famille();
    private List<Famille> lesFamilles = new ArrayList();
    @PersistenceContext
    EntityManager em;
    @Resource
    UserTransaction tx;

    @PostConstruct
    public void init() {

        Query query = em.createQuery("SELECT f FROM Famille f");
        lesFamilles = query.getResultList();

    }
    
    public void ajouterFamille(){
        System.out.println(this.famille.getNomFamille());
        FacesMessage message = null;

        message = new FacesMessage("Ajout validé");
        //ajouter le message JSF
        FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(this.famille);
            tx.commit();
            em.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        init();
    }

    public void modifierFamille(Famille f){
        FacesMessage message = null;
    message = new FacesMessage("Modification validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(em.merge(f));
            tx.commit();
            
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
    }

public void supprimerFamille(Famille f) {
    FacesMessage message = null;
    message = new FacesMessage("Suppression validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.remove(em.merge(f));
            tx.commit();
            em.close();
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
      init();
}

    public Famille getFamille() {
        return famille;
    }

    public void setFamille(Famille famille) {
        this.famille = famille;
    }

    public List<Famille> getLesFamilles() {
        return lesFamilles;
    }

    public void setLesFamilles(List<Famille> lesFamilles) {
        this.lesFamilles = lesFamilles;
    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public UserTransaction getTx() {
        return tx;
    }

    public void setTx(UserTransaction tx) {
        this.tx = tx;
    }
    
}
